//
//  AppDelegate+Service.swift
//  Kkmh-Swift
//
//  Created by tenltrs on 2018/10/25.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

extension AppDelegate{
    
    /// 初始化 日志
    func initLog() {
        
        #if DEBUG
        logger.setup(level: .debug, showLogIdentifier: false, showFunctionName: false, showThreadName: false, showLevel: true, showFileNames: true, showLineNumbers: false, showDate: true, writeToFile: nil, fileLevel: nil)
        #else
        logger.setup(level: .severe, showLogIdentifier: false, showFunctionName: false, showThreadName: false, showLevel: true, showFileNames: true, showLineNumbers: false, showDate: true, writeToFile: nil, fileLevel: nil)
        if let consoleLog = logger.destination(withIdentifier: XCGLogger.Constants.baseConsoleDestinationIdentifier)
            as? ConsoleDestination {
            consoleLog.logQueue = XCGLogger.logQueue
        }
        #endif
    }
    
    func setNetWorkMonitoring() {
        
        //declare this property where it won't go out of scope relative to your listener
        self.reachability = Reachability()
        
        //declare this inside of viewWillAppear
        
        NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: self.reachability)
        do{
            try self.reachability.startNotifier()
        }catch{
            logger.info("could not start reachability notifier")
        }
    }
    
    func setKeyboardManager() {
        
        IQKeyboardManager.shared.enable=true
        
        //控制点击背景是否收起键盘
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        
        //控制键盘上的工具条文字颜色是否用户自定义
        IQKeyboardManager.shared.shouldToolbarUsesTextFieldTintColor = true
        
        //将右边Done改成完成
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "完成"
        
        // 控制是否显示键盘上的工具条
        IQKeyboardManager.shared.enableAutoToolbar = true
        
        //最新版的设置键盘的returnKey的关键字 ,可以点击键盘上的next键，自动跳转到下一个输入框，最后一个输入框点击完成，自动收起键盘
        IQKeyboardManager.shared.toolbarManageBehaviour = .byPosition
    }
    
    @objc func reachabilityChanged(note: Notification) {
        
        let reachability = note.object as! Reachability
        
        switch reachability.connection {
        case .wifi:
            logger.info("WIFI")
        case .cellular:
            logger.info("蜂窝移动网络")
        case .none:
            logger.info("无网络")
        }
    }

}

